package com.zsg.webview.Adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zsg.webview.Config;
import com.zsg.webview.Listener.MyRecycleViewItemListener;
import com.zsg.webview.R;
import com.zsg.webview.WeiBoBaseClass.Status;
import com.zsg.webview.Util.Tools;

import java.util.ArrayList;
import java.util.List;

/**
 * RecycleView 适配器
 * Created by zzc on 2015/11/12.
 */
public class RcAdapter extends RecyclerView.Adapter implements Config {

    List<Status> data;
    Context context;
    LayoutInflater inflater;
    MyRecycleViewItemListener itemListener;

    static final int TEXT = 0;
    static final int IMAGE = 1;
    static final int IMAGESS = 2;
    static final int LAST = 3;

    float density;  //像素密度

    public RcAdapter(Context context, float density) {
        this.data = new ArrayList<>();
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.density = density;

    }

    public void registerItemClickListener(MyRecycleViewItemListener listener) {
        this.itemListener = listener;
    }


    /**
     * 更新数据
     *
     * @param data
     */
    public void updateData(List<Status> data, int type) {
        if (type == TYPE_REFRESH) {
            //刷新数据就清空 数组
            this.data.clear();
            this.data.addAll(data);
        } else if (type == TYPE_GETMORE) {
            //翻页 则在数组后面添加
            this.data.addAll(data);
        }
        this.notifyDataSetChanged();
    }

    /**
     * 获得类型的视图项
     *
     * @param position
     * @return
     */
    @Override
    public int getItemViewType(int position) {
        if (position == getItemCount() - 1)
            //最后一项 显示更多
            return LAST;

        Status status = data.get(position);
        //Log.d(MainActivity.TAG, status.pic_urls.size() + "");

        //根据 status中 多图数组的数量 来判断 微博类型
        if (status.pic_urls.size() == 0) {
            //文本
            return TEXT;
        } else if (status.pic_urls.size() == 1) {
            // 单张图片
            return IMAGE;
        } else {
            //多张图片
            return IMAGESS;
        }

    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        RecyclerView.ViewHolder holder = null;
        if (i == TEXT) {
            View v = inflater.inflate(R.layout.item_string, viewGroup, false);
            holder = new StringHolder(v, itemListener);
        } else if (i == IMAGE) {
            View v = inflater.inflate(R.layout.item_image, viewGroup, false);
            holder = new ImageHolder(v, itemListener);
        } else if (i == IMAGESS) {
            View v = inflater.inflate(R.layout.item_imagearr, viewGroup, false);
            holder = new ImageArrHolder(v, itemListener);
        } else {
            View v = inflater.inflate(R.layout.item_waitload, viewGroup, false);
            holder = new LoadHolder(v);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
        if (viewHolder instanceof LoadHolder) {
            ((LoadHolder) viewHolder).image.setVisibility(View.VISIBLE);
            ((LoadHolder) viewHolder).textView.setText("加载中");
            return;
        }

        Status status = data.get(i);
        MyHolder myHolder= (MyHolder) viewHolder;
        myHolder.text_name.setText(status.user.screen_name);

        //html中可以放图片
        myHolder.text_message.setText(Tools.changeTextToFace(context,
                Html.fromHtml(Tools.atBlue(status.text))));

        // holder.textg_message.setText(message);
        myHolder.about.setText("来自 " + status.getSource());
        //加载头像
        Picasso.with(context)
                .load(status.user.profile_image_url)
                .into(myHolder.image);

        myHolder.repostscount.setText(status.reposts_count+"");
        myHolder.commentcount.setText(status.comments_count+"");
        myHolder.praisecount.setText(status.attitudes_count+"");


        if (viewHolder instanceof StringHolder) {

            //StringHolder holder = (StringHolder) viewHolder;


        } else if (viewHolder instanceof ImageHolder) {
            ImageHolder holder = (ImageHolder) viewHolder;
            Picasso.with(context)
                    .load(status.user.profile_image_url)
                    .into(holder.image);
            //加载图片中等尺寸
            Picasso.with(context)
                    .load(status.bmiddle_pic)
                    .into(holder.imagepic);
            //String s= (String) status.pic_urls;
            //Log.d(MainActivity.TAG, status.pic_urls+"");

        } else if (viewHolder instanceof ImageArrHolder) {

            ImageArrHolder holder = (ImageArrHolder) viewHolder;

            Picasso.with(context)
                    .load(status.user.profile_image_url)
                    .into(holder.image);

            ArrayList<Status.PicURLS> urls = status.pic_urls;
            GridAdapter gridAdapter = holder.getGridAdapterInstance(context, urls);

            //设置gridview的高度 不然显示不出完整数据

            ViewGroup.LayoutParams params = holder.gridview.getLayoutParams();

            if (urls.size() <= 3) {
                params.height = (int) (90 * density);
            } else if (urls.size() > 3 && urls.size() < 7) {
                params.height = (int) (180 * density);
            } else if (urls.size() >= 7) {
                params.height = (int) (270 * density);
            }

            holder.gridview.setLayoutParams(params);
            holder.gridview.setAdapter(gridAdapter);


        }
        //Log.d(MainActivity.TAG, status.thumbnail_pic + " " + status.bmiddle_pic + " " + status.original_pic + " " + status.pic_urls);

    }

    @Override
    public int getItemCount() {
        return data.size() + 1;
    }

    public Status getItem(int position) {
        if (position < data.size()) {
            return data.get(position);
        }
        return null;
    }


    //单文字
    class StringHolder extends MyHolder {

        public StringHolder(View itemView, MyRecycleViewItemListener lis) {
            super(itemView, lis);

        }
    }

    //单图
    class ImageHolder extends MyHolder {
        ImageView imagepic;
        public ImageHolder(View itemView, MyRecycleViewItemListener lis) {
            super(itemView, lis);

            imagepic = (ImageView) itemView.findViewById(R.id.imagepic);
        }
    }

    //多图
    class ImageArrHolder extends MyHolder {
        GridView gridview;
        public ImageArrHolder(View itemView, MyRecycleViewItemListener lis) {
            super(itemView, lis);
            gridview = (GridView) itemView.findViewById(R.id.imagearr);
        }

        public GridAdapter getGridAdapterInstance(Context context, ArrayList<Status.PicURLS> data) {
            return new GridAdapter(context, data, GridAdapter.SM_PIC);
        }


    }

    //翻页 显示更多
    class LoadHolder extends RecyclerView.ViewHolder {

        ImageView image;
        TextView textView;

        public LoadHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.imageView);
            textView = (TextView) itemView.findViewById(R.id.textView);
        }
    }

    /**
     * 实现点击监听器  监听视图项
     */
    class MyHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        MyRecycleViewItemListener lis;
        ImageView image;        //头像
        TextView text_name;     //微博名
        TextView text_message; //文本内容
        TextView about;         //微博来自

        TextView repostscount;
        TextView commentcount;
        TextView praisecount;

        public MyHolder(View itemView, MyRecycleViewItemListener lis) {
            super(itemView);
            this.lis = lis;
            //为布局添加点击监听器  实现视图项点击监听
            itemView.setOnClickListener(this);
            image = (ImageView) itemView.findViewById(R.id.imageicon);
            text_name = (TextView) itemView.findViewById(R.id.name);
            text_message = (TextView) itemView.findViewById(R.id.message);
            about = (TextView) itemView.findViewById(R.id.about);

            repostscount = (TextView) itemView.findViewById(R.id.transmitcount);
            commentcount = (TextView) itemView.findViewById(R.id.commentcount);
            praisecount = (TextView) itemView.findViewById(R.id.praisecount);
        }


        @Override
        public void onClick(View v) {
            if (lis != null)
                //调用 自定义接口的方法  处理点击操作
                lis.onItemClick(v, getAdapterPosition());
        }
    }


}
